package com.bruce.pattern.creational.prototype;

import com.bruce.pattern.creational.prototype.clone.Pig;
import com.bruce.pattern.creational.prototype.clone.Pig2;
import com.bruce.util.DateUtils;
import com.bruce.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Date;

/**
 * Created by bruce on 2018/11/3 15:06
 */
@Slf4j
public class MainTest {

    @Test
    public void test1() throws CloneNotSupportedException {
        Mail mail = new Mail();
        mail.setContent("初始化模板");
        for (int i = 0; i < 10; i++) {
            Mail temp = (Mail) mail.clone();
            temp.setName("姓名" + i);
            temp.setAddresss("地址" + i);
            temp.setContent("您中将啦,快来领奖啊" + temp.hashCode());
            MailUtil.sendMail(temp);
        }
        log.info("原始内容不变:" + JsonUtils.toJson(mail));
    }


    //浅克隆
    @Test
    public void cloneTest1() throws CloneNotSupportedException {
        Date date = DateUtils.toDate("1992-06-24 15:12:13");
        Pig pig = new Pig("佩奇", date);
        Pig pig2 = pig.clone();

        System.out.println(pig);
        System.out.println(pig2);

        pig.setName("bruce");

        //如果是直接替换Date对象就不会出现浅克隆问题
        pig.getBirthday().setTime(666666666666L);

        log.info(JsonUtils.toJson(pig));
        log.info(JsonUtils.toJson(pig2));
    }

    //浅克隆
    @Test
    public void cloneTest2() throws CloneNotSupportedException {
        Date date = DateUtils.toDate("1992-06-24 15:12:13");
        Pig2 pig = new Pig2("佩奇", date);
        Pig2 pig2 = pig.clone();

        System.out.println(pig);
        System.out.println(pig2);

        pig.setName("bruce");

        //如果是直接替换Date对象就不会出现浅克隆问题
        pig.getBirthday().setTime(666666666666L);

        log.info(JsonUtils.toJson(pig));
        log.info(JsonUtils.toJson(pig2));
    }


}
